import { useMessage } from '@/hooks/web/useMessage';
import { DELETE, PAGE } from '@/api/http';
import { useRouter } from 'vue-router';

export function usePaddingOrder() {
  const { createConfirm } = useMessage();

  const router = useRouter();

  function toCashier(query: {
    memberCode: string;
    cardNo?: string;
    cardSubType?: string;
    takeId?: string;
  }) {
    router.push({
      path: '/cashier/index',
      query,
    });
  }

  return async function (option: {
    memberCode: string;
    cardNo: string;
    cardSubType: string;
    consumption?: string;
  }) {
    const { list } =
      (await PAGE('/cashier/getPendingOrder')({
        memberCode: option.memberCode,
        currentPage: 1,
        pageSize: 999999,
      })) ?? {};

    const target = list?.[0];
    if (!target) {
      toCashier?.(option);
    } else {
      createConfirm({
        iconType: 'info',
        content: '该客户还有未完成的挂单，是否继续开单？',
        okText: '继续开单',
        cancelText: '忽略，重新开单',
        onOk: async () => {
          toCashier({ memberCode: option.memberCode, takeId: target.id });
        },
        onCancel: async () => {
          await DELETE('/cashier/delPendingOrder', {
            memberCode: option.memberCode,
            id: target.id,
          });
          toCashier(option);
        },
      });
    }
  };
}
